package com.gm.server.voice.utils;

import javax.sound.sampled.*;

/**
 * 音频处理工具类
 */
public class AudioUtils {
    // 音频格式
    public static final AudioFormat AUDIO_FORMAT = new AudioFormat(
            8000.0f, // 采样率
            16,      // 采样位数
            1,       // 单声道
            true,    // 有符号
            false    // 小端
    );

    // 获取麦克风输入流
    public static TargetDataLine getMicrophone() throws LineUnavailableException {
        DataLine.Info info = new DataLine.Info(TargetDataLine.class, AUDIO_FORMAT);
        TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info);
        line.open(AUDIO_FORMAT);
        return line;
    }

    // 获取扬声器输出流
    public static SourceDataLine getSpeaker() throws LineUnavailableException {
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, AUDIO_FORMAT);
        SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
        line.open(AUDIO_FORMAT);
        return line;
    }
}